

package com.ce.pms.customer.service.impl;

import java.util.List;

import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.web.ICacheManage;
import com.ce.pms.customer.dao.ILesseeDao;
import com.ce.pms.customer.model.ClientDO;
import com.ce.pms.customer.model.ClientRHouseDO;
import com.ce.pms.customer.model.IndividualDO;
import com.ce.pms.customer.model.OrganizationDO;
import com.ce.pms.customer.service.ILesseeService;
import com.ce.pms.customer.service.IMemberService;
import com.ce.pms.customer.vo.CustomerHouseVO;
import com.ce.pms.customer.vo.CustomerVO;
import com.ce.pms.customer.vo.IndividualVO;
import com.ce.pms.customer.vo.OrganizationVO;
import com.ce.pms.house.dao.IFlatDao;
import com.ce.pms.house.model.HouseDomain;
import com.ce.util.BlankUtil;


/**
 * 功能描述:
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  许海堆
 * Created on  2008-6-4
 * @author
 * update on
 */
public class LesseeServiceImpl extends CustomerServiceImpl implements ILesseeService
{
	/**
	 * Function:
	 * @param
	 * @return String 返回true时，说明执行成功，返回其它值，侧说明出错，返回的值就是出错的提示信息
	 * Create author:xuhaidui
	 * Create on:2008-6-4
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	@Override
	public String addCustomer(List<CustomerHouseVO> houseList, List<CustomerVO> customerList){

		//根据客户类型和客户code，在数据字典中查找关于租户类型的实体
        SysrefcodeDO sysrefcodeDO = sysrefcodeDao.getSysrefCodesByCode("CS_CLIENT_CLIENT_TYPE", ClientDO.CT_LESSEE);

        for(CustomerHouseVO customerHouseVO : houseList){

	        for(CustomerVO customerVO:customerList){

	        	//操作的是个人
				if(CustomerVO.INDIVIDUAL_CODE.equals(customerVO.getParticipantTypeCode())){

					//如果操作状态是增加或者是修改
					if(ICacheManage.ADD.equals(customerVO.getOpratorStatus()) || ICacheManage.EDIT.equals(customerVO.getOpratorStatus())){

						//处理个人资料：保存个人资料
						IndividualDO individualDO=dealIndividual(customerVO);

						//更加个人的信息，建立客户角色和客户
						ClientDO clientDO = addOrEditClient(individualDO, sysrefcodeDO,customerVO.getCreator());

						//建立个人租户和房间之间的关系
						setClientRHouse(customerHouseVO, clientDO, customerVO.getCreator());

					}

				}

				//操作的是企业
				if(CustomerVO.ORGNIZATION_CODE.equals(customerVO.getParticipantTypeCode())) {

					//如果操作状态是增加或者是修改
					if(ICacheManage.ADD.equals(customerVO.getOpratorStatus()) || ICacheManage.EDIT.equals(customerVO.getOpratorStatus())){

						//处理企业资料：包括vo到do直接的copy，保存企业、法人、代理人，及建立法人和企业，企业和代理人直接的关系
						OrganizationDO organizationDO=dealOrgnization(customerVO);

						//更加企业的信息，建立客户角色和客户
						ClientDO clientDO = addOrEditClient(organizationDO, sysrefcodeDO,customerVO.getCreator());

						//建立企业和房间之间的关系
						setClientRHouse(customerHouseVO, clientDO, customerVO.getCreator());
					}

				}
				//如果房间或者租户其中有一个是删除状态
				if(ICacheManage.DELETE.equals(customerVO.getOpratorStatus()) || ICacheManage.DELETE.equals(customerHouseVO.getOpratorStatus())){
					ClientDO clientDO = null;
					OrganizationVO organizationVO = null;
					IndividualVO individualVO=null;
					OrganizationDO organizationDO = null;
					IndividualDO individualDO=null;
					Long lesseeId = null;
					if (CustomerVO.INDIVIDUAL_CODE.equals(customerVO.getParticipantTypeCode())) {
						individualVO = (IndividualVO)customerVO;
						//获得参与人的信息
						individualDO = individualDao.findById(individualVO.getParticipantId());
						lesseeId = individualDO.getParticipantId();
						clientDO = clientDao.queryClientByParticipantAndClientType(individualDO, sysrefcodeDO);
						//通过参与人信息和客户类型，获得客户信息
					}else if(CustomerVO.ORGNIZATION_CODE.equals(customerVO.getParticipantTypeCode())){
						organizationVO = (OrganizationVO)customerVO;
						//获得参与人的信息
						organizationDO = organizationDao.findById(organizationVO.getParticipantId());
						lesseeId = organizationDO.getParticipantId();
						//通过参与人信息和客户类型，获得客户信息
						clientDO = clientDao.queryClientByParticipantAndClientType(organizationDO, sysrefcodeDO);
					}

					// 解除一个客户与房屋的关系， 客户的家庭成员一并删除
					super.divestOneClientRHouse(clientDO, customerHouseVO,0);

				}

			}

        }


		return "true";
	}

	@Override
	public String editCustomer(List<CustomerHouseVO> houseList, List<CustomerVO> customerList) {
		return this.addCustomer(houseList, customerList);
	}

	/**
	 * Function:建立房间和客户之间的关系
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-13
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public ClientRHouseDO setClientRHouse(CustomerHouseVO customerHouseVO, ClientDO clientDO, Long oprator) {

		//更加房间的id和客户的id，获得房间客户关系实体
		ClientRHouseDO clientRHouseDO
		   = clientRHouseDao.getClientRHouseByHouseAndClient(customerHouseVO.getHouseId(), clientDO.getParticipantRoleId());

		//如果实体为空，则说明房间和客户还没有建立关系，那么新增一个
		if (BlankUtil.isBlank(clientRHouseDO)) {
			clientRHouseDO = new ClientRHouseDO();

			clientRHouseDO.setClient(clientDO);
			HouseDomain house= flatDao.findById(customerHouseVO.getHouseId());


			clientRHouseDO.setHouse(house);
			clientRHouseDO.setValidity(SysInfo.VALIDITY);


			clientRHouseDao.saveObject(clientRHouseDO);
		}
		return clientRHouseDO;
	}

	private ILesseeDao lesseeDao;

	private IFlatDao flatDao;

	public void setFlatDao(IFlatDao flatDao) {
		this.flatDao = flatDao;
	}

	public void setLesseeDao(ILesseeDao lesseeDao) {
		this.lesseeDao = lesseeDao;
	}

}
